package com.car.carmgr.controller;

import com.car.carmgr.bean.Car;
import com.car.carmgr.bean.Rent;
import com.car.carmgr.bean.User;
import com.car.carmgr.service.CarService;
import com.car.carmgr.service.CustomerService;
import com.car.carmgr.service.RentService;
import com.car.carmgr.util.AjaxResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

@RestController
@RequestMapping("business")
public class RentCarController {
    
    @Autowired
    private RentService rentService;

    @Autowired
    private CarService carService;

    @Autowired
    private CustomerService customerService;

    @RequestMapping("rent/rentRightJson")
    public AjaxResult rentRightJson(Integer page, Integer limit){
        PageInfo<Car> pageInfo = carService.findALL(page, limit);
        Integer count = carService.getCount();
        return AjaxResult.ok(count,pageInfo.getList());
    }

    @RequestMapping("rent/rentAdd")
    public AjaxResult rentAdd(Rent rent){
            int i = rentService.insertRent(rent);
            if(i>0){
                return AjaxResult.ADD_SUCCESS();
            }else{
                return AjaxResult.ADD_FAILED();
            }
    }

    @RequestMapping("rent/checkCustomer")
    public AjaxResult checkCustomer(String identity){
        int i = customerService.findCustomerById(identity);
        if(i>0){
            return AjaxResult.FIND_SUCCESS();
        }else{
            return AjaxResult.FIND_FAILED();
        }
    }

    @RequestMapping("rent/rentCodeJson")
    public String rentCodeJson(){
        String rentCode = "rent_";

        SimpleDateFormat sdf = new SimpleDateFormat("YYYYMMdd_HHmmss_SSS");
        String format = sdf.format(new Date());
        rentCode += format;

        rentCode += "-";

        Random random = new Random();
        for(int i = 0;i<5;i++){
            int i1 = random.nextInt(10);
            rentCode += i1;
        }
        return rentCode;
    }

    @RequestMapping("rentJson")
    public AjaxResult rentJson(Rent rent,Integer page,Integer limit){
        PageInfo<Rent> pageInfo = rentService.findAll(rent,page,limit);
        int count = rentService.getCount(rent);
        return AjaxResult.ok(count,pageInfo.getList());
    }
}
